package com.wsz.educms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsz.commonutils.R;
import com.wsz.educms.pojo.Banner;
import com.wsz.educms.service.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/educms/banneradmin")
@Api("后台的Banner管理")
public class BannerAdminController {

    @Autowired
    private BannerService bannerService;

    @GetMapping("/pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable Long page,@PathVariable Long limit){
        Page<Banner> page1 = new Page<>(page,limit);

        bannerService.page(page1, null);

        List<Banner> records = page1.getRecords();
        long total = page1.getTotal();

        return R.ok().data("total",total).data("items",records);
    }

    @ApiOperation("增加Banner")
    @PostMapping("/addBanner")
    public R addBanner(@RequestParam Banner banner) {

        bannerService.save(banner);

        return R.ok();
    }

    @ApiOperation(("修改Banner"))
    @PutMapping("/updateBanner")
    public R updateBanner(@RequestParam Banner banner) {
        bannerService.updateById(banner);
        return R.ok();
    }

    @ApiOperation("查Banner")
    @GetMapping("/getBanner/{id}")
    public R getBanner(@PathVariable String id){
        Banner byId = bannerService.getById(id);
        return R.ok().data("banner",byId);
    }

    @ApiOperation("删Banner")
    @DeleteMapping("/deleteBanner/{id}")
    public R deleteBanner(@PathVariable String id) {
        bannerService.removeById(id);
        return R.ok();
    }

}
